<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>商品详情</title>
</head>
<body>
	<?php
		header("Content-type:text/html;charset=utf-8;");
		include "mysqli.php";
	?>
	<h1>商品详情</h1>
	<?php
	$id=isset($_GET["id"])?$_GET["id"]:"1"; //判断id
	//根据id更新商品点击次数
	$sql="update goods set hits=hits+1 where id=".$id;
	$mysqli->query($sql);
	// 根据id查询商品
	$sql="select *from goods where id=".$id;
	$result=$mysqli->query($sql);

	// 对页面进行赋值
	while($row=$result->fetch_assoc()){
	?>
	<div>
		<span><?php echo $row["title"]?></span>
		<hr>
		<img src="<?echo $row["picurl"]?>" width="200" />
		<br>
		数量：- <input id="buynum" type="text" value="1" name=""> +
		<br>
		价格：<del>市场价：<?php echo $row["marketprice"]?></del>
		出售价：<b style="color: red"><?php echo $row["salesprice"]?></b>
		<input type="hidden" id="id" value="<?php echo $row["id"]?>" name="">
		<hr>
		内容：<?php echo $row["content"]?>
		<br>
		<a href="javascript:addshoppingcart('buy');">立即购买</a>
		<a href="javascript:addshoppingcart();">加入购物车</a>
	</div>
	<?php } ?>

	<script type="text/javascript" src="js/jquery.min.js"></script>
	<script type="text/javascript">
		// 立即购买：先添加到购物车再进行跳转到购买页面
		// 添加到购物车
		function addshoppingcart(a){
			$.ajax({
				url:"shoppingcart.php?a=addshoppingcart",
				type:"post",
				data:{
					'buynum':$("#buynum").val(),
					"id":$("#id").val()
				},
				dataType:"html",
				success:function(data){
					if(a=="buy"){
						location.href="shoppingcart.php?a=buynow";
					}else{
						if(data){
							alert('添加购物车成功！');
						}
					}
				}
			})
		}
	</script>

</body>
</html>